package com.mcxx.modules.temporary.order.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.family.dto.FamilyMemberDTO;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.order.entity.OrderStateEntity;
import com.mcxx.modules.temporary.family.dto.TmpFamilyMemberDTO;
import com.mcxx.modules.temporary.order.dto.TmpOrderDTO;
import com.mcxx.modules.temporary.order.dto.TmpOrderStateVerifyDTO;
import com.mcxx.modules.temporary.order.entity.TmpOrderEntity;
import com.mcxx.modules.temporary.order.dao.write.TmpOrderWriteDao;
import com.mcxx.util.FailureCode;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@TaTransactional
public class TmpOrderWriteService {

    private final static Logger log = LoggerFactory.getLogger(TmpOrderWriteService.class);

    @Autowired
    private TmpOrderWriteDao tmpOrderWriteDao;


    public void addOrder(TmpOrderEntity orderEntity) {
        int rows = tmpOrderWriteDao.insertIgnoreNull(orderEntity);
        if (rows < 1) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void updateOrder(TmpOrderEntity orderEntity) {
        int rows = tmpOrderWriteDao.updateIgnoreNull(orderEntity);
        if (rows < 1) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public int delete(TmpOrderEntity tmpOrder) {
    	return tmpOrderWriteDao.delete(tmpOrder);
    }


    public void updateOrderState(TmpOrderDTO orderDTO, String curBizState, String nextBizState){
        if(Constant.BizStateTmp.APPROVE_REJECT.equals(orderDTO.getState())
                || Integer.parseInt(curBizState) >= Integer.parseInt(nextBizState)){

            return;
        }
        OrderStateEntity orderStateEntity = new OrderStateEntity(orderDTO.getId(),
                nextBizState, orderDTO.getVersion());
        int rst = this.updateState(orderStateEntity);
        if(rst <= 0){
            log.info("orderId = {},说明修改状态信息失败", orderDTO.getId());
            throw new BizException("-1", "订单状态异常，修改失败");
        }
    }


    public void updateOrderState(List<TmpOrderDTO> orderDTOList, String curBizState, String preBizState){
        for(TmpOrderDTO orderDTO : orderDTOList){
            if(Constant.BizStateTmp.APPROVE_REJECT.equals(orderDTO.getState())){

                continue;
            }
            OrderStateEntity orderStateEntity = new OrderStateEntity(orderDTO.getId(), curBizState, preBizState, orderDTO.getVersion());
            int cnt = this.updateState(orderStateEntity);
            if(cnt <= 0){
                throw new BizException("-1", "修改业务的状态异常");
            }
        }
    }


    public int updateState(OrderStateEntity orderStateEntity){
        return tmpOrderWriteDao.updateState(orderStateEntity);
    }


    public void updateByIdAndVersion(TmpOrderEntity orderEntity) {
        int rows = tmpOrderWriteDao.updateByIdAndVersion(orderEntity);
        if (rows < 1) {
            throw new BizException(FailureCode.ERR_60000);
        }
    }


    public void updateOrderFamilyInfo(TmpFamilyMemberDTO familyMemberDTO, UserAccountVo accountVo, TmpOrderDTO orderDTO){
        TmpOrderEntity orderEntity = new TmpOrderEntity();

        orderEntity.setId(familyMemberDTO.getOrderId());

        orderEntity.setUpdateTime(new Date());

        orderEntity.setUpdateBy(accountVo.getUserId());

        if (Constant.DomesticRelation.ONESELF.equals(familyMemberDTO.getFamilyRelation())){
            orderEntity.setName(familyMemberDTO.getName());

            if(Constant.BizState.LOADINGOVER.equals(orderDTO.getBizState())){
                orderEntity.setBizState(Constant.BizState.SALVATION);
            }
        }

        orderEntity.setVersion(Integer.parseInt(orderDTO.getVersion()));

        orderEntity.setSaveMemberNumber(orderDTO.getSaveMemberNumber());

        updateByIdAndVersion(orderEntity);
    }


    public int updateRecordFlagById(@Param("idList") List<String> ids, @Param("recordFlag") String recordFlag){
        return tmpOrderWriteDao.updateRecordFlagById(ids,recordFlag);
    }

    public int updateBizStateByQueryCheck(List<String> list){return tmpOrderWriteDao.updateBizStateByQueryCheck(list);}


    public void updateMemberInfo(TmpFamilyMemberDTO  familyMemberDTO, String relation){
        TmpOrderEntity orderEntity = new TmpOrderEntity();

        if(Constant.IsApply.YES.equals(familyMemberDTO.getIsApply())){
            orderEntity.setId(familyMemberDTO.getOrderId());
            orderEntity.setApplyName(familyMemberDTO.getName());
            orderEntity.setApplyIdcard(familyMemberDTO.getIdcard());
        }

        if(Constant.DomesticRelation.ONESELF.equals(familyMemberDTO.getFamilyRelation())){
            orderEntity.setId(familyMemberDTO.getOrderId());
            orderEntity.setName(familyMemberDTO.getName());
            orderEntity.setIdcard(familyMemberDTO.getIdcard());
            orderEntity.setMemberId(familyMemberDTO.getId());
        }

        if(relation!=null && Constant.DomesticRelation.ONESELF.equals(relation) && !Constant.DomesticRelation.ONESELF.equals(familyMemberDTO.getFamilyRelation())){
            orderEntity.setId(familyMemberDTO.getOrderId());
            orderEntity.setName("");
            orderEntity.setIdcard("");
            orderEntity.setMemberId("");
        }
        if(StringUtils.isNotBlank(orderEntity.getId())){
            int rst = tmpOrderWriteDao.updateIgnoreNull(orderEntity);
            if(rst<1){
                throw new BizException(FailureCode.ERR_9999);
            }
        }
    }

    public int updateOrderState(TmpOrderStateVerifyDTO orderState){
        TmpOrderEntity orderEntity = new TmpOrderEntity();
        orderEntity.setId(orderState.getOrderId());
        orderEntity.setState(orderState.getNextState());
        orderEntity.setBizState(orderState.getNextBizState());
//        orderEntity.setVersion(Integer.valueOf(orderState.getVersion()));
        return tmpOrderWriteDao.updateIgnoreNull(orderEntity);
    }

    public int changeSaveMember(String orderId,String change){
        return tmpOrderWriteDao.changeSaveMember(orderId,change);
    }
    public int deleteById(String orderId){
        return tmpOrderWriteDao.deleteById(orderId);
    }


    public int deleteRecordById(String orderId) {
        return tmpOrderWriteDao.deleteRecordById(orderId);
    }
}
